package cn.edu.stu.syllabus.adapters;

import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;

import java.text.ParseException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.application.SyllabusApplication;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.repository.oa.local.OaInfoEntry;
import cn.edu.stu.syllabus.datas.OaCompany;
import cn.edu.stu.syllabus.utils.TimeUtils;

/**
 * Created by daidaijie on 2017/3/14.
 */

public class OaAdapter extends BaseQuickClickAdapter<OaInfoEntry, OaAdapter.ViewHolder> {

    private boolean mShowIsRead;

    @Override
    protected ViewHolder createBaseViewHolder(View view) {
        return new ViewHolder(view);
    }

    public OaAdapter(List<OaInfoEntry> data) {
        this(data, false);
    }

    public OaAdapter(List<OaInfoEntry> data, boolean showIsRead) {
        super(R.layout.item_oa, data);
        mShowIsRead = showIsRead;
    }

    public void clearRead() {
        List<OaInfoEntry> oaInfoEntries = getData();
        for (OaInfoEntry oaInfoEntry : oaInfoEntries) {
            oaInfoEntry.setRead(false);
        }
    }

    @Override
    protected void convert(final ViewHolder helper, final OaInfoEntry item) {


        helper.mOaItemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null && mClickable) {
                    mOnItemClickListener.onItemClick(item, helper.getAdapterPosition());
                }
            }
        });

        helper.mOaItemLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemLongClickListener != null && mClickable) {
                    return mOnItemLongClickListener.onItemLongClick(item, helper.getAdapterPosition());
                }
                return false;
            }
        });

        OaCompany oaCompany = OaCompany.getOaCompanyById(item.getMSubCompanyId());
        GradientDrawable shape = (GradientDrawable) ContextCompat
                .getDrawable(SyllabusApplication.getContext(), R.drawable.bg_theme_picker);
        String name;
        String shortName;
        if (oaCompany != null) {
            shape.setColor(oaCompany.getColor());
            name = oaCompany.getName();
            shortName = oaCompany.getShortName();
        } else {
            name = item.getMSubCompanyName();
            shortName = name.substring(0, 3);
        }

        helper.mSubCompanyShortNameTextView.setBackground(shape);
        helper.mSubCompanyShortNameTextView.setText(shortName);
        helper.mSubCompanyNameTextView.setText(name);

        String timeString = item.getMDate() + " " + item.getMTime();

        try {
            timeString = TimeUtils.formatTime(timeString, "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
        }
        helper.mTimeTextView.setText(timeString);
        helper.mTitleTextView.setText(item.getMTitle());

        if (mShowIsRead) {
            int colorDisable = ContextCompat.getColor(SyllabusApplication.getContext()
                    , R.color.colorTextDisable);
            if (item.isRead()) {
                helper.mTitleTextView.setTextColor(colorDisable);
                helper.mSubCompanyNameTextView.setTextColor(colorDisable);
            } else {
                int colorPrimary = ContextCompat.getColor(SyllabusApplication.getContext()
                        , R.color.colorTextPrimary);
                helper.mTitleTextView.setTextColor(colorPrimary);
                helper.mSubCompanyNameTextView.setTextColor(colorPrimary);
            }
        }
    }


    public void movePositionToTop(int position) {
        OaInfoEntry oaInfoEntry = getItem(position);
        remove(position);
        addData(0, oaInfoEntry);
    }

    public class ViewHolder extends BaseViewHolder {
        @Nullable
        @BindView(R.id.oaItemLayout)
        RelativeLayout mOaItemLayout;
        @Nullable
        @BindView(R.id.subCompanyShortNameTextView)
        TextView mSubCompanyShortNameTextView;
        @Nullable
        @BindView(R.id.subCompanyNameTextView)
        TextView mSubCompanyNameTextView;
        @Nullable
        @BindView(R.id.titleTextView)
        TextView mTitleTextView;
        @Nullable
        @BindView(R.id.timeTextView)
        TextView mTimeTextView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
